<template>
  <div class="project-view">
    <div>
      <el-form :inline="true" :model="condations" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="condations.username"
            placeholder="用户名称"
            clearable
          />
          <el-button type="primary" @click="submit">查询</el-button>
          <el-button @click="reset" type="info" round>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-table :data="userList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="70" />
        <el-table-column prop="username" label="名称" />
        <el-table-column prop="nickname" label="昵称" />
        <el-table-column prop="createTime" label="创建时间">
          <template v-slot="scope">
            {{ formatDate(scope.row.createTime, "YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间">
          <template v-slot="scope">
            {{ formatDate(scope.row.updateTime, "YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作" width="120">
          <template v-slot="scope">
            <el-button @click="resetPassword(scope.row)" type="info" round
              >重置密码</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { ref, onMounted } from "vue";
import { getUserListService, initUserPasswordService } from "../../api/userAPI";

// 查询条件
const condations = ref({
  username: "",
});

// 用户列表
const userList = ref([]);

// 查询
const submit = async () => {
  let data = await getUserListService(condations.value);
  userList.value = data.data;
};

// 重置查询
const reset = async () => {
  condations.value = {
    username: "",
  };

  let data = await getUserListService();
  userList.value = data.data;
};

// 重置密码
const resetPassword = async (data) => {
  let res = await initUserPasswordService({
    username: data.username,
  });
  ElMessage({
    message: res.message,
    type: res.code === 200 ? "success" : "error",
  });
};

onMounted(async () => {
  const res = await getUserListService(condations.value);
  userList.value = res.data;
});
</script>

<style scoped>
.demo-form-inline .el-input {
  --el-input-width: 200px;
}
</style>
